package net.blank.effect
{
	import flash.display.BitmapData;
	import flash.display.DisplayObjectContainer;
	import flash.display.LoaderInfo;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.events.Event;
	import flash.geom.Rectangle;
	
	import net.blank.bitmapOperation.BitmapDraw;
	import net.blank.blankLoader.ResourceItem;
	import net.blank.blankLoader.SequenceLoader;
	import net.blank.blankLoader.base.SwfLoader;
	import net.blank.timerUtl.PublicTimerManager;
	
	/**
	 * swf切片动画播放器
	 * @author blank
	 * 
	 */
	public class SwfClipsEffectPlayer
	{
		/**
		 * 总的播放轮数
		 */
		private var _count:int;
		
		/**
		 * 当前的播放的轮数
		 */
		private var _round:int;
		
		/**
		 * 播放完成后的移除延迟
		 */
		private var _removeDelayMs:int;
		
		/**
		 * 播放完成
		 */
		private var _playComplete:Function;
		
		/**
		 * 切片数据
		 */
		private var _clipData:Vector.<BitmapData>;
		
		/**
		 * 画板
		 */
		private var _canvas:Shape;
		
		/**
		 * 当前帧
		 */
		private var currentFrame:int;
		
		/**
		 * 总帧数
		 */
		private var totalFrames:int;
		
		/**
		 * 计算移除延迟
		 * 
		 */
		private function countRemoveDalay():void{
			PublicTimerManager.removeFunc(_removeDelayMs,countRemoveDalay);
			
			if(_playComplete != null){
				_playComplete();
			}
			
			remove();
		}
		
		/**
		 * 帧事件,主要用于判断特效是否播放完成
		 * @param evt
		 * 
		 */
		private function enterFrameHandler(evt:Event):void{
			if(currentFrame++ == totalFrames){
				_round++;
				
				if(_round >= _count){
					stop();
					
					if(_canvas.parent){
						if(_removeDelayMs <= 0){
							if(_playComplete != null){
								_playComplete();
							}
							
							remove();
						}else{
							PublicTimerManager.addFunc(_removeDelayMs,countRemoveDalay);
						}
					}
				}else{
					currentFrame=1;
				}
			}else{
				BitmapDraw.drawBitmap(_canvas.graphics,_clipData[currentFrame-1]);
			}
		}
		
		/**
		 * 停止播放
		 * 
		 */
		public function stop():void{
			_canvas.removeEventListener(Event.ENTER_FRAME,enterFrameHandler);
		}
		
		/**
		 * 移除当前特效
		 * 
		 */
		public function remove():void{
			if(_canvas){
				if(_canvas.parent){
					_canvas.parent.removeChild(_canvas);
				}
			}
		}
		
		/**
		 * 
		 * @param clipData
		 * @param x
		 * @param y
		 * @param playComplete
		 * @param count
		 * @param removeDelayMs
		 * 
		 */
		public function play(clipData:Vector.<BitmapData>,parent:DisplayObjectContainer,x:Number=0,y:Number=0,playComplete:Function=null,count:int=1,removeDelayMs:int=0):void{
			if(clipData.length){
				_clipData=clipData;
				_playComplete=playComplete;
				_count=count;
				_removeDelayMs=removeDelayMs;
				
				_round=0;
				
				_canvas=new Shape();
				if(parent){
					_canvas.x=x;
					_canvas.y=y;
					
					parent.addChild(_canvas);
				}
				
				currentFrame=1;
				totalFrames=clipData.length;
				_canvas.addEventListener(Event.ENTER_FRAME,enterFrameHandler);
			}
		}
		
		/**
		 * 获取类切片
		 * @param url
		 * @param clipComplete
		 * @param scrollRect
		 * @param sequenceLoader
		 * @param transparent
		 * 
		 */
		public static function getFrameClip(url:String,clipComplete:Function,scrollRect:Rectangle,sequenceLoader:SequenceLoader,transparent:Boolean=false):void{
			var $mc:MovieClip;
			var clipData:Vector.<BitmapData>;
			
			function enterFrameHandler(evt:Event):void{
				if($mc.currentFrame != $mc.totalFrames){
					var bmd:BitmapData=new BitmapData(scrollRect.width,scrollRect.height,transparent);
					bmd.draw($mc,null,null,null,scrollRect);
					
					clipData.push(bmd);
				}else{
					$mc.removeEventListener(Event.ENTER_FRAME,enterFrameHandler);
					
					if(clipComplete != null){
						clipComplete.apply(null,[clipData]);
					}
				}
			}
			
			if(sequenceLoader){
				sequenceLoader.addResource(
					new ResourceItem(
						ResourceItem.TYPE_DLL,
						url,
						0,null,false,
						function(li:LoaderInfo):void{
							$mc=li.content as MovieClip;
							clipData=new Vector.<BitmapData>();
							
							$mc.addEventListener(Event.ENTER_FRAME,enterFrameHandler);
							$mc.gotoAndPlay(1);
						}
					)
				);
			}
			sequenceLoader.startLoad();
		}
		
		/**
		 * 获取类切片(ios不支持此格式)
		 * @param url
		 * @param clipComplete
		 * @param classPrefix
		 * 
		 */
		public static function getClassClip(url:String,clipComplete:Function,classPrefix:String="cls"):void{
			var clipData:Vector.<BitmapData>;
			
			var swfL:SwfLoader=new SwfLoader(
				url,
				function(li:LoaderInfo):void{
					clipData=new Vector.<BitmapData>();
					
					var n:int=1;
					while(true){
						var bmd:*;
						try{
							bmd=SwfLoader.getInstanceFromLoaderInfo(classPrefix+n,li);
							
							clipData[n-1]=bmd;
						}catch(err:Error){
							if(clipComplete != null){
								clipComplete.apply(null,[clipData]);
							}
							
							break;
						}
						n++;
					}
				}
			);
			swfL.load();
		}
	}
}